package com.web.transaction;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.domain.TreatmentTransaction;
import com.service.TransactionService;
import com.util.AuthenticateHttpServlet;
import com.util.AuthenticateSession;
import com.util.PageMaker;

@WebServlet("/Transaction/Treatment/Search")
public class TreatmentSearchTransaction extends AuthenticateHttpServlet {

	@Override
	protected void post(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String queryStr = "";
		String strCode = request.getParameter("transactionId");
		System.out.println("transactionId:" + strCode);
		if (strCode != null) {
			if (!strCode.trim().equals("")) {
				int transactionId = Integer.parseInt(strCode);
				if (transactionId != 0) {
					queryStr += " transactionId=" + transactionId + " ";
				}
			}
		}
		String employeeId = request.getParameter("employeeId");
		if (employeeId != null) {
			if (!employeeId.trim().equals("")) {
				if (!queryStr.equals("")) {
					queryStr =  queryStr + " and ";
				}
				queryStr += " employeeId='" + employeeId + "' ";
			}
		}
		AuthenticateSession session = (AuthenticateSession) request
				.getSession().getAttribute("authenticateSession");
		if (session != null) {
			request.setAttribute("user", session);
			TransactionService service = new TransactionService();
			if (!queryStr.equals("")) {
				queryStr = " where " + queryStr;
			}
			List<TreatmentTransaction> list = service.searchTransactionByQuery(queryStr);
			PageMaker<TreatmentTransaction> maker = new PageMaker<TreatmentTransaction>(10);
			list = maker.getCurrentPageList(list, 0);
			request.setAttribute("treatments", list);
			request.getRequestDispatcher(
					"/WEB-INF/jsp/treatmentSearchTransaction.jsp").forward(
					request, response);
		} else {
			request.getRequestDispatcher("/login.jsp").forward(request,
					response);
		}
	}

}
